/* eslint-disable default-case */
import { message, Modal } from 'antd';
import moment from 'moment';
import _ from 'lodash';
import {
  careerList,
  contactAddList,
  getCareerInfor,
  getCareerMatInfor,
  Add,
  countpay,
  getDictCodeList,
  getjoinIdeList,
  updateExce,
  Update,
  getProData,
  signPro,
  entryContAddList,
  updateSubmit,
  areaScopeList,
  contTypeList,
  getDownloadInfo,
  updateBaseData,
  getUploadProgress,
  updateChareData,
  getVerifyMatPkg,
  uploadFileBatch,
  getSelectMkuPay,
  addEntryMkupPay,
  delEntryMkupPay,
} from '@/services/careermange';
import * as service from '@/services/socket';
import { SocketActions, batchTaskFactory } from '@/models/socket';
import { delay } from '@/utils/dva';
// 非常重要属性
const namespace = 'career';

// let typeName;

export default {
  namespace,
  state: {
    data: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    activeKey: '1',
    tabList: [
      { itemCode: '1', itemName: '待办理' },
      { itemCode: '2', itemName: '办理中' },
      { itemCode: '3', itemName: '已办理' },
      { itemCode: '5', itemName: '问题人员' },
      { itemCode: '4', itemName: '已取消' },
    ],
    formFieldsValue: {},
    downloadParams1: [],
    pageOrdBys: [{ property: null, ordBy: null }],
    expandForm: false,
    detail: {
      basicDto: {},
      chargeDto: {},
      consultRcrds: [],
      entryContRcrds: [],
      matSubmitInfos: [],
      ordHdlRemarks: [],
      sendDto: {},
      serDto: {},
      mkupPayDets: [],
    },
    careerMatInfor: {
      basicDto: {},
      chargeDto: {},
      consultRcrds: [],
      entryContRcrds: [],
      matSubmitInfos: [],
      mkupPayDets: [],
      ordHdlRemarks: [],
      sendDto: {},
      serDto: {},
      editingKey: '',
      elementArr: [],
      oldAssArr: [],
      matFullFlag: null,
      matPkgVo: {},
    },
    insAssDictList: [],
    accuAssDictList: [],
    insJoinTypeDictList: [],
    accuJoinTypeDictList: [],
    assDictList: [],
    pdDictList: [],
    joinTypeDictList: [],
    chrgFlagDictList: [],
    pdArrList: [],
    matPay: {},
    dictCode: [],
    proObjData: [],
    proVisible: false,
    index: 0,
    subObj: {},
    areaList: [],
    contList: [],
    batchQuery: {
      // 批量上传数据
      areaId: null,
      contType: null,
      progressPercent: 0,
    },
    currentStep: 0,
    progressPercent: 0,
    resultInfo: {
      // 结果数据必传
      batchName: '', // 任务名称
      taskCount: 0, // 导入数据
      successCount: 0, // 成功数据
      errorCount: 0, // 失败数据
      batchNo: '', // 批次号
      errorUrl: '/esApi/matSubmit/expEntryHdlError', // 下载错误信息地址
      errorParams: null, // 下载错误数据参数
    },
    showProgress: false,
    joinIdeId: null,
    rows: [],
    keys: [],
  },
  effects: {
    // 获取列表
    *fetchList({ payload }, { call, put }) {
      const response = yield call(careerList, payload);
      const {
        pagerArg: { currentPage, pageSize },
      } = payload;
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    // 联系新增
    *contactAdd({ payload, callback }, { call, put }) {
      const response = yield call(contactAddList, payload);
      yield put({
        type: 'updateModelData',
        payload: {
          batchId: response.data.batchId,
          contactWay: payload.contactWay,
          progressPercent: 0,
        },
      });
      if (response.success) {
        if (response.data && response.data.successProp !== 100) {
          // typeName = 'index';
          yield put({
            type: 'updateModelData',
            payload: {
              showProgress: true,
            },
          });
          const { batchId } = response.data;
          const batchTask = batchTaskFactory(batchId, namespace, 'handleBatchTask');

          yield put.resolve({
            type: `socket/${SocketActions.subSocketChannel}`,
            payload: {
              batchTask,
            },
          });
        } else {
          yield put({
            type: 'updateIndexBatchTask',
            payload: response.data,
          });
        }
        callback(response);
      }
    },
    // 入职联系新增
    *entryContAdd({ payload, callback }, { call, put }) {
      const response = yield call(entryContAddList, payload);
      if (response.success) {
        callback(response);
        yield put({
          type: 'entryContAddList',
          payload: response,
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    // 收材料 联系记录
    *contactAddColle({ payload, callback }, { call, put }) {
      const response = yield call(entryContAddList, payload);
      if (response.success) {
        callback(response);
        yield put({
          type: 'entryContAddListColl',
          payload: response,
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    // 详情
    *getCareer({ payload }, { call, put }) {
      const response = yield call(getCareerInfor, payload);
      if (response.success) {
        yield put({
          type: 'getDetail',
          payload: response.data,
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    // 收材料 详情
    *getCareerMat({ payload, callback }, { call }) {
      const response = yield call(getCareerMatInfor, { entryId: payload.entryId });
      if (callback) {
        callback(response);
      }
      if (!response.success) {
        message.warning(response.message || '服务端错误');
      }
    },
    // 收材料
    add:[
      function* ({ callback }, { call, select }) {
        const subObj = yield select(state => state.career.subObj);
        const response = yield call(Add, subObj);
        if (response.success) {
          callback(response);
        } else {
          message.warning(response.message || '服务端错误');
        }
      },
      { type:'throttle', ms:10000 }
    ],
    // 修改材料
    update:[
      function* ({ callback }, { call, select }) {
        const subObj = yield select(state => state.career.subObj);
        const response = yield call(Update, subObj);
        if (response.success) {
          callback(response);
        } else {
          message.warning(response.message || '服务端错误');
        }
      },
      { type:'throttle', ms:10000 }
    ],
    // 提交运行
    updateSub:[
      function* ({ payload, callback }, { call, select }) {
        const subObj = yield select(state => state.career.subObj);
        const response = yield call(updateSubmit, { ...payload, ...subObj });
        if (response.success) {
          callback(response);
        } else {
          message.warning(response.message || '服务端错误');
        }
     },
      { type:'throttle', ms:10000 }
    ],
    // 计算补缴
    *assCount({ payload, callback }, { call, select }) {
      const serId = yield select(state => state.career.serId);
      const response = yield call(countpay, { ...payload.opermars, entryId: serId });
      if (response.success) {
        callback(response);
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    // 获取字典
    *getDictCode({ payload }, { call, put }) {
      const response = yield call(getDictCodeList, payload);
      if (response.success) {
        yield put({
          type: 'getDictCodeList',
          payload: response.data,
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    *getJoinIde({ payload }, { call, put }) {
      const response = yield call(getjoinIdeList, payload);
      if (response.success) {
        yield put({
          type: 'getjoinIdeList',
          payload: response.data,
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    // 提交运行
    updateExceState:[
      function* ({ payload, callback }, { call }) {
      const response = yield call(updateExce, payload);
      if (response.success) {
        callback(response);
      } else {
        Modal.error({
          title: '提交失败',
          content: response.message,
          okText: '关闭',
          okType: 'default',
          autoFocusButton: 'cancel',
        });
      }
    },
    { type:'throttle', ms:10000 }
  ],
    // 获取问题对象
    *fetchProData({ payload }, { call, put }) {
      const response = yield call(getProData, payload);
      if (response.success) {
        // 查询问题对象
        yield put({
          type: 'getProDataList',
          payload: response.data,
        });
        // 显示弹框
        yield put({
          type: 'toggleProVisible',
          payload: { proVisible: true },
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    // 标记问题人员
    *signPro({ payload, callback }, { call, put }) {
      const response = yield call(signPro, payload);
      if (response.success) {
        if (!response.data) {
          message.success('标记成功');
          yield put({
            type: 'toggleProVisible',
            payload: { proVisible: false },
          });
          if (callback) {
            callback(response);
          }
        } else if (response.data.success) {
          Modal.warning({
            title: '标记成功',
            content: response.data.message,
            okText: '关闭',
            okType: 'default',
            autoFocusButton: 'cancel',
            onOk: () => {
              if (callback) callback(response);
            },
          });
          yield put({
            type: 'toggleProVisible',
            payload: { proVisible: false },
          });
        } else if (!response.data.success) {
          Modal.error({
            title: '标记失败',
            content: response.data.message,
            okText: '关闭',
            okType: 'default',
            autoFocusButton: 'cancel',
            onOk: () => {
              if (callback) callback(response);
            },
          });
          yield put({
            type: 'toggleProVisible',
            payload: { proVisible: false },
          });
        }
      } else {
        yield put({
          type: 'toggleProVisible',
          payload: { proVisible: false },
        });
        Modal.error({
          title: '标记失败',
          content: response.message,
          okText: '关闭',
          okType: 'default',
          autoFocusButton: 'cancel',
        });
      }
    },
    // 获得地区
    *getAreaScope({ payload }, { call, put }) {
      const response = yield call(areaScopeList, payload);
      if (response.success) {
        yield put({
          type: 'areaScopeList',
          payload: response.data,
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    // 合同类型
    *getContTypeScope({ payload }, { call, put }) {
      const response = yield call(contTypeList, payload);
      if (response.success) {
        yield put({
          type: 'contTypeList',
          payload: response.data,
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    *selectMkuPay({ payload }, { call, put, select }) {
      const serId = yield select(state => state.career.serId);
      const response = yield call(getSelectMkuPay, { ...payload, entryId: serId });
      if (response.success) {
        yield put({
          type: 'updateModelData',
          payload: response.data,
        });
        return response;
      }
      return message.warning(response.message || '服务端错误');
    },
    *getDownloadInfo({ payload }, { call, put }) {
      const response = yield call(getDownloadInfo, payload);
      yield put({
        type: 'updateQuery',
        payload,
      });
      if (response.success) {
        const fileInfo = response.data.data.split('|');
        yield put({
          type: 'publicdata/setFileDownload',
          payload: { fileId: fileInfo[0], fileName: fileInfo[1] },
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    // 提交基础信息
    *updateBase({ payload, callback }, { call, put }) {
      const response = yield call(updateBaseData, payload);
      if (response.success) {
        yield put({
          type: 'updateBaseData',
          payload: response.data,
        });
        const resAgin = yield call(getCareerMatInfor, { entryId: payload.entryId });
        if (resAgin.success) {
          yield put({
            type: 'updateDataBase',
            payload: resAgin.data.basicDto,
          });
        } else {
          message.warning(response.message || '服务端错误');
        }
        callback(response);
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    // 提交收费信息
    *updateChare({ payload, callback }, { call, put }) {
      const response = yield call(updateChareData, payload);
      if (response.success) {
        yield put({
          type: 'updateChareData',
          payload: response.data,
        });
        const resAgin = yield call(getCareerMatInfor, { entryId: payload.entryId });
        if (resAgin.success) {
          yield put({
            type: 'updateDataChare',
            payload: resAgin.data,
          });
        } else {
          message.warning(response.message || '服务端错误');
        }
        callback(response);
      } else {
        message.warning(response.message || '服务端错误');
        callback(response);
      }
    },
    *pdChange({ payload }, { put, select }) {
      const { pdName } = payload;
      const {
        insAssDictList,
        accuAssDictList,
        insJoinTypeDictList,
        accuJoinTypeDictList,
      } = yield select(state => state.career);

      yield put({
        type: 'updateModelData',
        payload: {
          assDictList: pdName === '社保' ? insAssDictList : accuAssDictList,
          joinTypeDictList: pdName === '社保' ? insJoinTypeDictList : accuJoinTypeDictList,
        },
      });
    },
    // 进度条
    *getUploadProgress({ payload, callback }, { call, put }) {
      const response = yield call(getUploadProgress, payload);
      if (response.success) {
        yield put({
          type: 'updateQuery',
          payload: { progressPercent: response.data.successProp },
        });
      }
      if (callback && typeof callback === 'function') {
        callback(response);
      }
    },
    // 材料校验
    *fetchVerifyMatPkg({ payload }, { call, put }) {
      const response = yield call(getVerifyMatPkg, payload);
      yield put({
        type: 'updateQuery',
        payload,
      });
      return response;
    },
    // 匹配唯一batchId
    *matchBatchId({ payload }, { put, select }) {
      const { batchId } = yield select(state => state.career);
      if (+batchId === +payload.batchId) {
        yield put({
          type: 'handleBatchTask',
          payload,
        });
      }
    },
    uploadFileWithProgress: [
      // 上传文件:批量进度
      function* uploadFileWithProgress({ payload }, { call, put }) {
        const response = yield call(uploadFileBatch, payload);
        if (response.success) {
          if (response.data && response.data.successProp !== 100) {
            // yield call(service.socketChannel, {
            //   type: 'sub',
            //   data: { to: response.data && (response.data.batchId || response.data), type: 'task' },
            // });

            const { batchId } = response.data;
            const batchTask = batchTaskFactory(batchId, namespace, 'handleBatchTask');

            yield put.resolve({
              type: `socket/${SocketActions.subSocketChannel}`,
              payload: {
                batchTask,
              },
            });
            // 订阅成功后再触发进度条
            yield put({
              type: 'updateModelData',
              payload: { showProgress: true, batchId: response.data.batchId },
            });
          } else {
            yield put({
              type: 'handleBatchTask',
              payload: response.data,
            });
          }
          // yield put({
          //   type: 'updateBatchTask',
          //   payload: response.data,
          // });
        } else {
          // 上传失败
          message.warning(`${response.message || '上传失败'}`);
        }
        return response;
      },
      { type: 'throttle', ms: 10000 },
    ],

    *handleBatchTask({ payload }, { put, select }) {
      const state = yield select(s => s[namespace]);
      let { showProgress, resultInfo, currentStep } = state;
      const { batchQuery } = state;
      showProgress = true;
      if (payload.successProp >= 100) {
        resultInfo.errorParams = {
          batNo: payload.batchId,
          areaId: batchQuery.areaId,
          contTypes: batchQuery.contTypes,
        };
        resultInfo = { ...resultInfo, ...payload };
        currentStep += 1;
        showProgress = false;
        // 先更新进度
        yield put({
          type: 'updateBatchTask',
          payload: {
            progressPercent: payload.successProp,
            resultInfo,
          },
        });
        yield delay(3000);
        // 再更新状态
        yield put({
          type: 'updateBatchTask',
          payload: {
            currentStep,
            showProgress,
          },
        });
      } else {
        yield put({
          type: 'updateBatchTask',
          payload: { progressPercent: payload.successProp, resultInfo },
        });
      }
    },

    // 更新首页批量任务数据
    *updateIndexBatchTask({ payload }, { put, select }) {
      const { current, pageSize } = yield select(state => state.career.data.pagination);
      const { downloadParams1, activeKey, contactWay } = yield select(state => state.career);
      if (payload.successProp >= 100) {
        const pagerArg = { currentPage: current, pageSize };
        if (Number(activeKey) === 5) {
          yield put({
            type: 'dimission/fetchProList',
            payload: {
              ...downloadParams1,
              pagerArg,
              busiType: 1,
            },
          });
        } else {
          yield put({
            type: 'fetchList',
            payload: {
              ...downloadParams1,
              currentPage: current,
              pageSizes: pageSize,
            },
          });
        }
        if (payload.errorCount === 0) {
          Modal.success({
            title: +contactWay === 1 ? '操作成功' : '发送成功',
            okText: '关闭',
            okType: 'default',
            autoFocusButton: 'cancel',
          });
        }
        if (+contactWay !== 1) {
          // 全部失败
          if (payload.successCount === 0) {
            Modal.error({
              title: '发送失败',
              okText: '关闭',
              okType: 'default',
              autoFocusButton: 'cancel',
              content:
                +contactWay === 2
                  ? '因短信通知模板未配置，未发送成功，请公司管理员完成配置'
                  : '因邮件通知模板未配置或员工没有邮箱信息，发送失败，请联系公司管理员完成配置或完善邮箱信息',
            });
          }
          // 部分失败
          if (payload.successCount !== 0 && payload.errorCount !== 0) {
            Modal.info({
              title: `发送成功，成功${payload.successCount}条，失败${payload.errorCount}条`,
              okText: '关闭',
              okType: 'default',
              autoFocusButton: 'cancel',
              content:
                +contactWay === 2
                  ? '因短信通知模板未配置，部分数据未发送成功，请公司管理员完成配置'
                  : '因邮件通知模板未配置或员工没有邮箱信息，部分数据未发送成功，请联系公司管理员完成配置或完善邮箱信息',
            });
          }
        }
        yield put({
          type: 'updateModelData',
          payload: {
            showProgress: false,
          },
        });
      } else {
        yield put({
          type: 'updateBatchTask',
          payload,
        });
      }
    },

    // 添加个人补缴
    *addPayInfor({ payload }, { call, put }) {
      const response = yield call(addEntryMkupPay, payload);
      if (response.success) {
        message.success('操作成功');
        yield put({
          type: 'updateEntryMkupPayList',
          payload: response,
        });
        return response;
      }
      return message.warning(response.message || '服务端错误');
    },
    // 删除个人补缴
    *delPayInfor({ payload }, { call, put }) {
      const response = yield call(delEntryMkupPay, payload);
      if (response.success) {
        message.success('删除成功');
        yield put({
          type: 'updateEntryMkupPayList',
          payload: response,
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
  },
  reducers: {
    // 更新批量任务数据
    updateBatchTask(state, { payload }) {
      // const { batchQuery } = state;
      // let { showProgress, resultInfo, currentStep } = state;
      // showProgress = true;
      // if (payload.successProp === 100) {
      //   resultInfo.errorParams = {
      //     batNo: payload.batchId,
      //     areaId: batchQuery.areaId,
      //     contTypes: batchQuery.contTypes,
      //   };
      //   resultInfo = { ...resultInfo, ...payload };
      //   currentStep += 1;
      //   showProgress = false;
      // }
      return {
        ...state,
        ...payload,
        // showProgress,
        // progressPercent: payload.successProp,
        // resultInfo,
        // currentStep,
      };
    },
    // 更新批量上传参数
    updateQuery(state, { payload }) {
      return {
        ...state,
        batchQuery: { ...state.batchQuery, ...payload },
      };
    },
    // 更新数据
    updateData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 更新数据
    updateDataBase(state, { payload }) {
      return {
        ...state,
        careerMatInfor: { ...state.careerMatInfor, basicDto: payload },
      };
    },
    // 更新数据
    updateDataChare(state, { payload }) {
      return {
        ...state,
        careerMatInfor: {
          ...state.careerMatInfor,
          ...payload,
          pdChargeData: _.cloneDeep(payload.chargeDto.pdChargeDtos),
          ordHdlRemarks: payload.ordHdlRemarks,
        },
      };
    },
    // 数据处理
    inOpermars(state, { payload }) {
      const { serId, careerMatInfor } = state;
      const entryInfo = {};
      let ordHdlRemark = {};
      const matSubmitInfos = [];
      Object.entries(payload).forEach(([key]) => {
        if (key.indexOf('_') === -1) {
          entryInfo.mkupPayFlag =
            (payload.mkupPayDets && payload.mkupPayDets.length) ||
            (payload.mkupPayDets && Object.keys(payload.mkupPayDets).length)
              ? 1
              : 2;
          entryInfo.coltFlag = careerMatInfor.matPkgVo.coltFlag;
          if (String(key) === 'content') {
            ordHdlRemark = {
              content: payload.content,
            };
          }
        } else if (key.split('_')[0] === 'mat') {
          if (payload[key].inpType && String(payload[key].inpType) === '9') {
            let file = '';
            const tempObj = payload[key];
            if (tempObj.fileList && tempObj.fileList.length) {
              tempObj.fileList.map(item => {
                if (item.response && item.response.success) {
                  file += `${item.response.data},`;
                } else if (!item.response) {
                  file += item.uid
                    .concat('|')
                    .concat(item.name)
                    .concat(',');
                }
                return true;
              });
            }
            tempObj.inpVal = file ? file.slice(0, file.length - 1) : '';
          }
          if (payload[key].inpType && String(payload[key].inpType) === '11') {
            const tempObj = payload[key];
            tempObj.inpVal =
              tempObj.areaValue && tempObj.areaValue.length
                ? tempObj.areaValue[tempObj.areaValue.length - 1]
                : '';
            delete tempObj.areaValue;
            delete tempObj.provCityAreaMatList;
            delete tempObj.targetOption;
            delete tempObj.parentId;
          }
          matSubmitInfos.push(payload[key]);
        }
      });
      const arr = [];
      // 补缴
      if (payload.mkupPayDets) {
        Object.entries(payload.mkupPayDets).forEach(([key, value]) => {
          const tempObj = value;
          tempObj.beginDate = value.beginDate ? String(moment(value.beginDate).valueOf()) : null;
          tempObj.endDate = value.endDate ? String(moment(value.endDate).valueOf()) : null;
          if (key.length > 5) {
            arr.push({ ...value, mkupPayId: key });
          } else {
            arr.push(value);
          }
        });
      }
      careerMatInfor.elementArr.forEach(item => {
        if (Number(item.openFlag) === 2) {
          matSubmitInfos.push(item);
        }
      });
      const subObj = {
        serId,
        matFullFlag: payload.matFlag,
        entryInfo,
        matSubmitInfos,
        ordHdlRemark,
        mkupPayDets: arr,
      };
      return {
        ...state,
        subObj,
      };
    },
    entryContAddList(state, { payload }) {
      const tempObj = state;
      tempObj.detail.consultRcrds = payload.data.consultRcrds;
      tempObj.detail.entryContRcrds = payload.data.entryContRcrds;

      return {
        ...state,
      };
    },
    entryContAddListColl(state, { payload }) {
      const { consultRcrds, entryContRcrds } = payload.data;
      return {
        ...state,
        careerMatInfor: {
          ...state.careerMatInfor,
          entryContRcrds,
          consultRcrds,
        },
      };
    },

    updateEntryMkupPayList(state, { payload }) {
      // const { consultRcrds, entryContRcrds } = payload.data;
      return {
        ...state,
        careerMatInfor: {
          ...state.careerMatInfor,
          mkupPayDets: payload.data || [],
          // entryContRcrds,
          // consultRcrds,
        },
      };
    },

    // 获取列表
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        data: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    // 获取详情
    getDetail(state, { payload }) {
      const newPayload = payload;
      newPayload.serDto.detailSerDtos = payload.serDto.detailSerDtos.filter(item => item.ass !== 1);
      newPayload.chargeDto.pdChargeDtos = payload.chargeDto.pdChargeDtos.filter(
        item => item.ass !== 1
      );
      return {
        ...state,
        detail: newPayload,
      };
    },
    // 获取字典
    getDictCodeList(state, { payload }) {
      return {
        ...state,
        dictCode: payload,
      };
    },
    // 处理数据
    getCareerMatInfor(state, { payload }) {
      const { resData } = payload;
      const elementArr = [];
      const maGpages =
        resData.matPkgVo && resData.matPkgVo.maGpages ? resData.matPkgVo.maGpages : [];
      if (maGpages.length) {
        maGpages.map(item => {
          if (item.maGpDetails && item.maGpDetails.length) {
            item.maGpDetails.map(d => {
              const obj = {};
              if (d.children) {
                obj.children = d.children;
                // children里第一层地区值
                obj.children.map(c => {
                  const newC = c;
                  if (Number(c.inpType) === 11) {
                    newC.areaValue = c.inpVal ? c.inpVal.split(',') : undefined;
                    newC.name = d.inpValName;
                  }
                  return true;
                });
              }
              // 是否必填
              obj.reqFlag = d.reqFlag;
              obj.matName = d.matName;
              obj.inpType = d.inpType;
              obj.relBusiType = d.relBusiType;
              obj.openFlag = d.openFlag;
              obj.pageDetailId = d.pageDetailId;

              // obj.url='';
              if (d.matSubmitInfoId) {
                obj.matSubmitInfoId = d.matSubmitInfoId;
              }
              obj.matId = d.matId;
              obj.dictItems = d.dictItems;
              if (d.inpVal) {
                obj.inpVal = d.inpVal;
              } else {
                obj.inpVal = d.dftVal;
              }
              if (Number(d.inpType) === 9) {
                const fileArr = d.inpVal ? d.inpVal.split(',') : [];
                const fileList = [];
                Object.keys(fileArr).forEach(key => {
                  const tempObj = {
                    uid: fileArr[key].split('|')[0],
                    name: fileArr[key].split('|')[1],
                    status: 'done',
                  };
                  fileList.push(tempObj);
                });
                obj.fileList = fileList;
              }
              if (Number(d.inpType) === 11) {
                obj.areaValue = d.inpVal ? d.inpVal.split(',') : undefined;
                obj.name = d.inpValName;
              }
              elementArr.push(obj);
              return true;
            });
          }
          return true;
        });
      }
      elementArr.map((element, i) => {
        const newElement = element;
        // 第一层
        if (element.dictItems) {
          element.dictItems.map(nameDic => {
            if (String(element.inpVal) === String(nameDic.itemCode)) {
              newElement.name = nameDic.itemName;
            }
            return true;
          });
        }
        // 第二层
        if (element.children && element.children.length) {
          element.children.map(item => {
            if (element.children.dictItems) {
              element.children.dictItems.map(nameDic => {
                const newItem = item;
                if (String(item.inpVal) === String(nameDic.itemCode)) {
                  newItem.name = nameDic.itemName;
                }
                return true;
              });
            }
            if (item.judgeLogicCode === element.inpVal) {
              const index = i + 1;
              elementArr.splice(index, 0, item);
            }
            return true;
          });
        }
        return true;
      });
      // 如果修改可以改
      const oldAssArr = [];
      if (resData.mkupPayDets.length) {
        resData.mkupPayDets.map(item => {
          const tempItem = item;
          oldAssArr.push(item.ass);
          if (Number(item.mkupPayPlcy) === 1) {
            tempItem.status = true;
          } else {
            tempItem.status = false;
          }
          return true;
        });
      }
      // 开始办理页面的办理完成弹窗数据
      let probFlag;
      let submitExecFlag;
      const socialArr =
        resData.serDto &&
        resData.serDto.entryInsDetDto &&
        resData.serDto.entryInsDetDto.entryDetDtos;
      const accuArr =
        resData.serDto &&
        resData.serDto.entryAccuDetDto &&
        resData.serDto.entryAccuDetDto.entryDetDtos;
      const insMergFlag =
        resData.serDto &&
        resData.serDto.entryInsDetDto &&
        resData.serDto.entryInsDetDto.insMergFlag;
      if (Number(insMergFlag) === 1) {
        probFlag = socialArr.length && socialArr[0].probFlag;
        submitExecFlag = socialArr.length && socialArr[0].submitExecFlag;
      }
      const pdArrList = {
        accuArr,
        socialArr,
        probFlag,
        submitExecFlag,
        insMergFlag,
      };
      const pdChargeData = _.cloneDeep(resData.chargeDto.pdChargeDtos);
      const newArr = resData.mkupPayDets;
      if (pdChargeData.length) {
        pdChargeData.map(item => {
          const newItem = item;
          if (newArr.length) {
            newArr.map(value => {
              if (Number(item.ass) === Number(value.ass)) {
                newItem.assDisabled = true;
              }
              return true;
            });
          }
          return true;
        });
      }
      return {
        ...state,
        careerMatInfor: { ...resData, elementArr, pdArrList, oldAssArr, pdChargeData },
      };
    },
    // 数据处理
    listData(state, { payload }) {
      const assList = Array.isArray(payload.entryDetails) ? payload.entryDetails : [];
      const socialArr = [];
      const accuArr = [];
      let probFlag;
      let submitExecFlag;
      if (assList && assList.length) {
        assList.map(item => {
          if (Number(item.ass) === 2) {
            accuArr.push(item);
          }
          if (Number(item.ass) !== 2) {
            socialArr.push(item);
          }
          return true;
        });
      }
      if (Number(payload.insMergFlag) === 1) {
        probFlag = socialArr.length && socialArr[0].probFlag;
        submitExecFlag = socialArr.length && socialArr[0].submitExecFlag;
      }
      return {
        ...state,
        pdArrList: {
          execState: payload.execState,
          insMergFlag: payload.insMergFlag,
          accuArr,
          socialArr,
          probFlag,
          submitExecFlag,
        },
      };
    },
    // 材料处理
    matInfor(state, { payload }) {
      let { elementArr } = payload;
      const { element } = payload;
      // 可选择 情况下
      if (String(element.inpType) === '4' || String(element.inpType) === '11') {
        let index;
        elementArr.map((elem, i) => {
          if (elem.pageDetailId === element.pageDetailId) {
            index = i;
          }
          return true;
        });
        if (element.children && element.children.length) {
          element.children.map(item => {
            if (item.judgeLogicCode === element.inpVal) {
              index += 1;
              elementArr.splice(index, 0, item);
            } else {
              elementArr = elementArr.filter(ele => ele.matId !== item.matId);
            }
            return true;
          });
        }
      }
      const tempState = state;
      tempState.careerMatInfor.elementArr = elementArr;
      return {
        ...state,
      };
    },

    // 更新数据
    matFlagChange(state, { payload }) {
      return {
        ...state,
        careerMatInfor: {
          ...state.careerMatInfor,
          matFullFlag: payload,
        },
      };
    },

    disMku(state, { payload }) {
      if (Number(payload.value) === 1) {
        state.careerMatInfor.mkupPayDets.map(item => {
          const tempItem = item;
          if ((item.mkupPayId || item.id) === payload.id) {
            tempItem.status = true;
          }
          return true;
        });
      } else {
        state.careerMatInfor.mkupPayDets.map(item => {
          const tempItem = item;
          if ((item.mkupPayId || item.id) === payload.id) {
            tempItem.status = false;
          }
          return true;
        });
      }
      return {
        ...state,
      };
    },
    // 日期修改
    dateChange(state, { payload }) {
      state.careerMatInfor.chargeDto.pdChargeDtos.map(item => {
        const tempItem = item;
        if (item.id === payload.id) {
          tempItem.sendBeginDate = payload.dateString;
        }
        return true;
      });
      return {
        ...state,
      };
    },
    // 获取问题对象
    getProDataList(state, { payload }) {
      const tempArr = [];
      payload.map(item => {
        tempArr.push({ label: item.itemName, value: item.itemCode });
        return true;
      });
      return {
        ...state,
        proObjData: tempArr,
      };
    },
    // 切换问题标记弹框的显隐
    toggleProVisible(state, { payload }) {
      return {
        ...state,
        proVisible: payload.proVisible,
      };
    },
    // 设置页签状态
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    areaScopeList(state, { payload }) {
      return {
        ...state,
        areaList: payload,
      };
    },
    contTypeList(state, { payload }) {
      return {
        ...state,
        contList: payload,
      };
    },
  },
  subscriptions: {
    socket({ dispatch }) {
      // return service.listen(action => {
      //   switch (action.type) {
      //     case 'task':
      //       if (typeName === 'index') {
      //         dispatch({
      //           type: 'matchBatchId',
      //           payload: action.data,
      //         });
      //       } else {
      //         dispatch({
      //           type: 'updateBatchTask',
      //           payload: action.data,
      //         });
      //       }
      //       // 全局保存
      //       dispatch({
      //         type: 'publicdata/updateBatchTaskData',
      //         payload: action.data,
      //       });
      //       break;
      //   }
      // });
      service.listen(async action => {
        // 判断是否自己命名空间的回调

        if (action.namespace === namespace) {
          switch (action.type) {
            case 'task':
              dispatch({
                type: 'matchBatchId',
                payload: action.data,
              });
              await dispatch({
                type: `socket/${SocketActions.handleUpdateBatchTask}`,
                payload: {
                  successProp: action.data.successProp,
                  batchId: action.data.batchId,
                },
              });
              // 全局保存
              dispatch({
                type: 'publicdata/updateBatchTaskData',
                payload: action.data,
              });
              break;
          }
        }
      }, namespace);
    },
  },
};
